<?php
use yii\helpers\Url;

$web = Url::to('@web');
$t = \app\models\Permission::hasPermission('finance', 'audit-confirm-handler');
?>
<script type="application/javascript">

    var isRefresh = 0;
    var insCode = 0;
    var insSelectINS;
    var invHeadSelected;
    var isAudit;
    $(function ($) {
        loading.hide();
        isAudit ='<?=  \app\models\Permission::hasPermission("finance", "audit-confirm-handler") ?>';
        insCode = "<?= $institution ?>";
        $("#select1").val("<?= $status ?>");

        if ("<?= $filter ?>" == 0) {
            $('#search1').val("");
        } else {
            $('#search1').val("<?= $filter ?>");
        }


        if ("<?= $startTime ?>" == 0) {
            $('#startTime').val("");
        } else {
            $('#startTime').val("<?= $startTime ?>");
        }

        if ("<?= $endTime ?>" == 0) {
            $('#endTime').val("");
        } else {
            $('#endTime').val("<?= $endTime ?>");
        }

        var institution =  <?= json_encode($institutionList) ?>;
        insSelectINS = $('#selectINS').treeAutocomplete({
            defaultText: '----请选择机构----',
            source: institution,
            onSelected: function (element) {
                insChange();
            }
        });

    });

    //新增审核发票申请
    function addModifyInvoice(id) {
        $("#addInvoice").modal('show');
        $("#alert1").css("display", "none");
        $("#alert2").css("display", "none");
        $("#alert3").css("display", "none");
        $("#alert4").css("display", "none");
        var data = {};
        data.id = id;

        data.invHead = insSelectINS.getText();

        data.invIns = $("#selectINS").val();
        //loading.show();
        requestJson("add-audit-invoice-handler", data, true, function (re) {
            if (re.status == 0) {
                //$("#selectINS").val(re.data.result.institution_code);
                insSelectINS.setValue(re.data.result.institution_code);
                $("#invoiceHead").val(re.data.result.invoice_head);
                $("#selectTYPE").val(re.data.result.invoice_type);
                $("#invoiceMoney").text(re.data.result.invoiced);
                $("#totalMoney").text(re.data.result.recharged);
                if (re.data.recharged - re.data.invoiced >= 0) {
                    $("#unInvoiceMoney").text(re.data.recharged - re.data.invoiced);
                } else {
                    $("#unInvoiceMoney").text(0);
                }
                $("#selectCat option").each(function () {
                    if ($(this).text() == re.data.result.category_name) {
                        $(this).attr('selected', true);
                    }
                });
                $("#unit").val(re.data.result.unit);
                $("#pcount").val(re.data.result.pcount);
                $("#pMoney").val(re.data.result.p_money);
                $("#fpDate").val(re.data.result.fp_date);
                $("#remark").val(re.data.result.remark);
                $("#auditRemark").val(re.data.result.audit_remark);
                $("#applayUser").text(re.data.result.applay_name);
                $("#applayTime").text(re.data.result.applay_time);
                $("#auditUser").text(re.data.result.audit_name);
                $("#auditTime").text(re.data.result.audit_time);
                if (re.data.result.audit_status == 1) {
                    $("#auditStatus").text('通过');
                } else if (re.data.result.audit_status == 2) {
                    $("#auditStatus").text('失败');
                } else {
                    $("#auditStatus").text('待审核');
                    $("#auditUser").text("");
                    $("#auditTime").text("");
                }

                //新增机构可更改否则不允许更改
                if (id) {
                    //$("#selectINS").attr("disabled", true);
                    insSelectINS.setDisabled(true);
                    $("#save").css("visibility", "hidden");
                    $("#invoiceHead").attr("disabled", true);
                    $("#selectTYPE").attr("disabled", true);
                    $("#selectCat").attr("disabled", true);
                    $("#unit").attr("disabled", true);
                    $("#pcount").attr("disabled", true);
                    $("#pMoney").attr("disabled", true);
                    $("#fpDate").attr("disabled", true);
                    $("#remark").attr("disabled", true);
                    $("#invoiceId").val(id);

                } else {
                    if (insCode == '0') {
                        //$("#selectINS").attr("disabled", false);
                        insSelectINS.setDisabled(false);
                    } else {
                        //$("#selectINS").attr("disabled", true);
                        insSelectINS.setDisabled(true);
                        insChange();
                    }

                    $("#save").css("visibility", "visible");
                    $("#invoiceHead").attr("disabled", false);
                    $("#selectTYPE").attr("disabled", false);
                    $("#selectCat").attr("disabled", false);
                    $("#unit").attr("disabled", false);
                    $("#pcount").attr("disabled", false);
                    $("#pMoney").attr("disabled", false);
                    $("#fpDate").attr("disabled", false);
                    $("#remark").attr("disabled", false);
                }

                //已审核过的可以看到审核相关信息，否则不能看到
                if (re.data.result.audit_status) {
                    $("#auditRemarkDiv").css("display", "block");
                    $("#userDiv").css("display", "block");
                } else {
                    $("#auditRemarkDiv").css("display", "none");
                    $("#userDiv").css("display", "none");
                }

                //发票申请人机构和当前登录人员不为同一机构，表示审核，否则表示查看
                if (re.data.result.institution_code != re.data.institution && re.data.result.audit_status == 0 && id != 0 && isAudit) {
                    $("#auditPass").css("visibility", "visible");
                    $("#auditFail").css("visibility", "visible");
                } else {
                    $("#auditPass").css("visibility", "hidden");
                    $("#auditFail").css("visibility", "hidden");
                }
            } else {
                alert(re.message);
            }
        });
    }

    //申请发票，更改机构，同步显示所选机构的已开票等信息
    function insChange() {
        var data = {};

        data.invHead = insSelectINS.getText();

        $("#invoiceHead").val(data.invHead);
        data.invIns = $("#selectINS").val();
        requestJson("uninvoice-handler", data, true, function (re) {
            $("#invoiceMoney").text(re.data.invoiced);
            $("#totalMoney").text(re.data.recharged);

            if (re.data.recharged - re.data.invoiced >= 0) {
                $("#unInvoiceMoney").text(re.data.recharged - re.data.invoiced);
            } else {
                $("#unInvoiceMoney").text(0);
            }
        });
    }


    //确认新增发票申请
    function addConfirm() {
        var data = {};
        data.applyIns = $("#selectINS").val();
        data.invHead = $("#invoiceHead").val();
        data.fp_date = $("#fpDate").val();
        data.selectTYPE = $("#selectTYPE").val();
        data.category_name = $("#selectCat").find("option:selected").text();
        data.unit = $("#unit").val();
        data.pcount = $("#pcount").val();
        data.p_money = $("#pMoney").val();
        data.remark = $("#remark").val();
        if (data.applyIns == 0 || data.applyIns == '') {
            $("#alert3").css("display", "block");
        } else if (data.invHead == '') {
            $("#alert1").css("display", "block");
        } else if (data.p_money == '' || isNaN(data.p_money)) {
            $("#alert4").css("display", "block");
        } else {
            requestJson("add-confirm-handler", data, true, function (re) {
//                var text = "申请失败！";
                if (re.status == 0) {
//                    text = "申请成功！";
                    window.location.reload();
                } else {
                    alert('申请失败!');
                }
//                $("#message").text(text);
//                $("#institutionModal3").modal('show');
//                isRefresh = 0;
            });
        }
    }

    //审核申请
    function auditConfirm(flag) {
        //审核失败，必须填审核失败原因
        var text = $("#auditRemark").val();
        if (flag == 2 && text == '') {
            $("#alert2").css("display", "block");
        } else {
            var data = {};
            data.id = $("#invoiceId").val();
            data.flag = flag;
            data.auditRemark = text;
            requestJson("audit-confirm-handler", data, true, function (re) {
//                var text = "操作失败！";
                if (re.status == 0) {
//                    text = "操作成功！";
//                    alert('操作成功!');
                    window.location.reload();
                } else {
                    alert('操作失败!');
                }
//                $("#message").text(text);
//                $("#institutionModal3").modal('show');
//                isRefresh = 1;
            });
        }
    }

    //删除发票申请
    function deleteInv(id) {
        var data = {};
        data.id = id;
        data.flag = 3;
        requestJson("del-confirm-handler", data, true, function (re) {
            //var text = "删除失败！";
            if (re.status == 0) {
                alert("删除成功！");
                isRefresh = 0;
                refresh();
            } else {
                alert("删除失败！");
            }

            /*$("#message").text(text);
             $("#institutionModal3").modal('show');*/


        });
    }

    //搜索
    function search() {
        var status = $("#select1").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        var filter;
        var f = $('#search1').val()._trim();
        /*if (f == '') {
         filter = 0;
         } else {
         filter = f;
         }*/
        var href = window.location.href;
        if (f != '') {
            href = setUrlParam(href, 'filter', f);
        }
        if (startTime != '') {
            href = setUrlParam(href, 'startTime', startTime);
        }
        if (endTime != '') {
            href = setUrlParam(href, 'endTime', endTime);
        }
        if (status != 99) {
            href = setUrlParam(href, 'status', status);
        }
        //var href_str = href + "?filter=" + filter + "&startTime=" + startTime + "&endTime=" + endTime + "&status=" + status;
        window.location.href = href;
    }

    //刷新页面
    function refresh() {
        loading.show();
        if (isRefresh != 0) {
            location.reload();
        } else {
            var href = window.location.href;
            var index = href.indexOf("?");
            if (index > 0) {
                href = href.substr(0, index);
            }
            window.location.href = href;
        }
        isRefresh = 0;
    }

    //设置模态窗:当用户点击模态框外部时不会关闭、当初始化时不显示模态框。
    $(function () {
        $('#addInvoice').modal({
            backdrop: 'static', show: false
        });
    });
</script>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group col-lg-2">
                <a class="btn btn-default " type="button" data-toggle="modal" onclick="addModifyInvoice(0)">
                    <i class="fa fa-plus"></i>申请开票</a>
            </div>

            <div class="pull-right col-lg-10">
                <div class="form-group  col-lg-6">
                    <div class="input-group custom-date-range" data-date-format="yyyy-mm-dd">
                        <input type="text" class="form-control dpd1" name="from" id="startTime" placeholder="开始时间">
                        <span class="input-group-addon">To</span>
                        <input type="text" class="form-control dpd2" name="to" id="endTime" placeholder="结束时间">
                    </div>
                </div>
                <div class="form-group col-lg-2">
                    <select class="form-control" id="select1">
                        <option value="99">状态</option>
                        <option value="0">待审核</option>
                        <option value="1">审核通过</option>
                        <option value="2">审核失败</option>
                    </select>
                </div>
                <div class="form-group col-lg-3">
                    <input type="text" class="form-control" id="search1" placeholder="请输入机构名称或申请编码"
                           onkeypress="if(event.keyCode==13) {search();return false;}">
                </div>
                <div class="btn-group col-lg-1">
                    <button type="submit" class="btn btn-primary" onclick="search()">
                        <i class="fa fa-search"></i> 搜索
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>发票申请编码</th>
                    <th>机构</th>
                    <th>开票额度</th>
                    <th>开票申请人</th>
                    <th>开票时间</th>
                    <th>审核状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($items as $item) {
                    ?>
                    <tr>
                        <td><?= $item['pk_code'] ?></td>
                        <td><?= $item['institution_name'] ?></td>
                        <td><?= $item['p_money'] ?></td>
                        <td><?= $item['applay_name'] ?></td>
                        <td><?= $item['applay_time'] ?></td>
                        <td><?php if ($item['audit_status'] == 1) {
                                echo '<span class="label label-success">审核通过</span>';
                            } else if ($item['audit_status'] == 2) {
                                echo '<span class="label label-danger">审核失败</span>';
                            } else {
                                echo '<span class="label label-warning">待审核</span>';
                            }
                            ?>
                        </td>
                        <td>
                            <div class="btn-group">
                                <button class="btn btn-link btn-xs" type="button"
                                        onclick="addModifyInvoice('<?= $item['id'] ?>')">详情
                                </button>
                                <?php if ($item['audit_status'] == 0) {
                                    echo '<button class="btn btn-link btn-xs" type="button"  onclick="deleteInv(\'' . $item['id'] . '\')">删除 </button>';
                                }
                                ?>

                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<!-- 新增、审核开票申请-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="addInvoice" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">添加/审核发票申请</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal adminex-form">
                    <div class="form-group">
                        <label class="col-lg-3 control-label"><span style="color: red">*</span>申请机构</label>
                        <div class="col-lg-9">
                            <div id="selectINS"></div>
                            <label id='alert3' style="display: none;"><span style="color: red">请选择一个机构</span></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label"><span style="color: red">*</span>发票抬头</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="invoiceHead">
                            <input class="hidden_id" id="invoiceId" type="hidden" value="0"/>
                            <label id='alert1' style="display: none;"><span style="color: red">发票抬头不允许为空</span></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">开票类型</label>
                        <div class="col-lg-9">
                            <select class="form-control m-bot15" id="selectTYPE">
                                <option value="0">普通发票</option>
                                <option value="1">增值税普通发票</option>
                                <option value="2">增值税专用发票</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">品名</label>
                        <div class="col-lg-9">
                            <select class="form-control m-bot15" id="selectCat">
                                <option value="0">技术服务费</option>
                                <option value="1">办公用品</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">单位</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="unit">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">数量</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="pcount" value="1">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label"></label>
                        <div class="col-lg-3">
                            <label class="control-label">充值金额：<label id="totalMoney">0</label></label>
                        </div>
                        <div class="col-lg-3">
                            <label class="control-label">已开票金额：<label id="invoiceMoney">0</label></label>
                        </div>
                        <div class="col-lg-3">
                            <label class="control-label">未开票金额：<label id="unInvoiceMoney">0</label></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label"><span style="color: red">*</span>申请开票金额</label>
                        <div class="col-lg-9">
                            <input type="text" class="form-control" id="pMoney">
                            <label id='alert4' style="display: none;"><span
                                    style="color: red">请填写开票金额,且金额不允许为非数字。</span></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">所需发票日期</label>
                        <div class="col-lg-9">
                            <input class="form-control dpd2" data-format="yyyy-MM-dd" type="text" id="fpDate"
                                   placeholder="请选择日期">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">备注</label>
                        <div class="col-lg-9">
                            <textarea rows="6" class="form-control" id="remark"></textarea>
                        </div>
                    </div>
                    <div class="form-group" id="auditRemarkDiv">
                        <label class="col-lg-3 control-label">审核批注</label>
                        <div class="col-lg-9">
                            <textarea rows="6" class="form-control" id="auditRemark"></textarea>
                            <label id='alert2' style="display: none;"><span style="color: red">请填写审核批注！</span></label>
                        </div>
                    </div>
                    <div id="userDiv">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">申请人：<label id="applayUser"></label></label>
                            <label class="col-sm-4 control-label">申请时间：<label id="applayTime"></label></label>
                            <label class="col-sm-3 control-label">审核状态：<label id="auditStatus"></label></label>
                        </div>
                        <div class="form-group">

                            <label class="col-sm-3 control-label">审核人：<label id="auditUser"></label></label>
                            <label class="col-sm-4 control-label">审核时间：<label id="auditTime"></label></label>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <div class="pull-left">
                    <button class="btn btn-success" id="auditPass" onclick="auditConfirm(1)">审核通过</button>
                    <button class="btn btn-danger" id="auditFail" onclick="auditConfirm(2)">审核失败</button>
                </div>
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
                <button class="btn btn-primary" id="save" onclick="addConfirm()">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 提示框 -->
<div aria-hidden="true" role="dialog" tabindex="-1" id="institutionModal3" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">消息</h4>
            </div>
            <div class="modal-body" id="message">
                操作失败！
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" onclick="refresh()"> Ok</button>
            </div>
        </div>
    </div>
</div>